$(function() {
    // 使用搜索条
    weui.searchBar("#searchBar");
    var s = $.trim(Common.fn.getUrlParam('s'));
    // 存在输入的值
    if(s) {
        var result = Model.business.searchBusiness({
            condition: s
        });
        if(result.code) {
            Common.fn.formatOutput(result);
            loading.hide();
            return false;
        }
        for(var i = 0;i < result.data.length;i++) {
            var item = '<div class="weui-cell search-item" data-id="' + result.data[i].businessId + '"><div class="shop-vendor-left"><div class="portrait-block"><img class="portrait-img" src="' + result.data[i].photo + '"></div></div><div class="weui-cell__hd"><label class="vendor-level">v' + result.data[i].grade + '</label></div><div class="weui-cell__bd"><h3 class="text-1-6">' + result.data[i].nickname + '</h3></div><div class="weui-cell__ft"><a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_default attention-btn" data-id="' + result.data[i].businessId + '" data-attention="' + result.data[i].attention + '">';
            if (result.data[i].attention == true) {
                // 如果已关注
                item += '已关注</a></div></div>';
            }else {
                item += '关注</a></div></div>';
            }
            $('.s-content').append(item);
        }
    }
});

// 关注按钮
$(document).on('click', '.attention-btn', function() {
    var state = $(this).attr('data-attention'),
        id = $(this).attr('data-id');
    if (state == 'true') {
        // 已关注
        weui.confirm(
            "真的要取消关注吗？",
            function () {
                // 取消关注
                result = Model.business.attendBusiness({
                    businessId: id
                });
                if (result.code) {
                    alert(result.message);
                    location.reload();
                    return false;
                }
                location.reload();
            },
            function () { }
        );
    } else {
        // 关注店铺
        var loading = weui.loading('loading', {
            className: 'custom-classname'
        });
        result = Model.business.attendBusiness({
            businessId: id
        });
        if (result.code) {
            Common.fn.formatOutput(result);
            loading.hide();
            return false;
        }
        loading.hide();
        location.reload();
    }
});

// 店铺跳转
$(document).on('click','.search-item', function() {
    location.href = 'shop_detail.html?id=' + $(this).attr('data-id');
});